//
//  SPEdit.swift
//  Example
//
//  Created by admin on 2020/9/1.
//  Copyright © 2020 admin. All rights reserved.
//

import SwiftUI
import RealmSwift

struct SPStorePlan: View {
    
    var pid:String
    var name:String = ""
    @State var showUpdate = false
    @ObservedObject var moden = SPStorePlanModen()

    func delete(index:Int)  {
        let storedata =  self.moden.plans[index]
        self.moden.plans.remove(at: index)
        StoreDataItem.ug_delete(id: storedata.id)
    }
    
    
    var body: some View {
        
        ZStack {
            List {
                
                ForEach(moden.plans) { item in
                    
                    planitemView(item: item)
                }
                .onDelete { index in
                    self.delete(index: index.first!)
                }
            }
            
            VStack{
                Spacer()
                
                Button(action: {
                    self.showUpdate.toggle()
                }) {
                    Image(systemName: "plus")
                        .imageScale(.large)
                        .foregroundColor(Color.white)
                        .font(.system(size: 16, weight: .medium))
                        .frame(width: 36, height: 36)
                        .background(Color("accent").opacity(0.8))
                        .clipShape(Circle())
                        .shadow(color: Color.black.opacity(0.1), radius: 1, x: 0, y: 1)
                        .shadow(color: Color.black.opacity(0.2), radius: 10, x: 0, y: 10)
                }
                    
                .sheet(isPresented: $showUpdate) {
                    SPStorePlanCreate( pid: self.pid)
                }
                
            }
            
            
        }
        .navigationBarTitle(name)
        .onAppear {
            self.moden.update(pid:self.pid)
        }
    }
}


struct SPStorePlan_Previews: PreviewProvider {
    static var previews: some View {
        
        SPStorePlan(pid:storeData.id)
    }
}
var storeData = try! Realm().objects(StoreData.self)[0]



struct planitemView: View {
    
    var item:StoreDataItem
    
    func getPrice(price:String,count:String,index:Int,step:Double) ->Double{
          guard let numprice = Double(price) else { return 0.0 }
          let bili = Double(index)*step
          return numprice*(1+bili)
      }
      
      func getshouyi(price:String,count:String,index:Int,step:Double) ->Double{
          guard let numprice = Double(price) else { return 0.0 }
          guard let numcount = Double(count) else { return 0.0 }
          let bili = Double(index) * step
          return  (numprice*(1+bili) - numprice)  * numcount
      }
    
    var body: some View {
        VStack(){
            
            HStack() {
                
                Text(item.ccount)
                    .font(.system(size: 22))
                    .foregroundColor(.white)
                    .padding(5)
                
                Text(item.price)
                    .font(.system(size: 17))
                    .foregroundColor(.white)
                
                
                Spacer()
            }
            
            Divider().background(Color.white)
                .scaleEffect(CGSize(width: 1, height: 1))
            
            ScrollView(.horizontal, showsIndicators: false) {
                HStack(){
                    ForEach(1..<5) { i in
                        VStack(){
                            
                            Text("\(self.getPrice(price:self.item.price,count:self.item.ccount,index:i,step:0.05), specifier: "%.3f")")
                                .font(.system(size: 18))
                                .foregroundColor(.white)
                                .padding(5)
                            Text("\(0.05*Double(i)*100.0, specifier: "%.0f")%")
                                .font(.system(size: 12))
                                .foregroundColor(.white)
                            
                            
                            Text("收益：\(self.getshouyi(price:self.item.price,count:self.item.ccount,index:i,step:0.05), specifier: "%.2f")")
                                .font(.system(size: 12))
                                .foregroundColor(.white)
                                .padding(5)
                        }
                        .frame(width: 100,height: 80)
                        .background(Color("accent"))
                            
                        .cornerRadius(8)
                        .padding(8)
                            
                        .shadow(color: Color(#colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)).opacity(0.5), radius: 5, x: 5, y: 5)
                    }
                }
            }
                
            .cornerRadius(8)
            
        }
            
        .background(Color("accent"))
        .cornerRadius(8)
    }
}
